import pickle
import pandas as pd
from pandas import DataFrame
import sys


class ObjMemory (): #处理对象读取和存储

    def LoadObj (self,path): #载入对象
        Obj = None

        with open (path,'rb+') as ObjFile:
            Obj = pickle.load(ObjFile)

        return Obj

    
    def SaveObj (self,Obj,path): #保存对象
        with open (path,'wb+') as ObjFile:
            pickle.dump(Obj,ObjFile)


class UnTaggedAction (): #Untagged_Action
    OM = ObjMemory()

    def __init__(self) -> None:
        self.Base = None
        self.Data = None
        self.actions = []
        self.solutions = []
        self.activatings = []
        self.solution_start = 0
        self.col = 0
        self.datasize = 0
        self.next_id = 0

        self.Base = self.OM.LoadObj('./System/TrainAction.pkl')
        self.Data = self.Base.Data
        self.Data['Sample_id'] = 0
        self.Data['Result_Text'] = ''
        self.Data = self.Data.iloc[0:0,:]
        self.actions = self.Base.actions
        self.solutions = self.Base.solutions
        self.activatings = self.Base.activatings
        self.solution_start = self.Base.solution_start
        self.col = self.Data.shape[1]
        self.datasize = 0


    def Update (self): #更新数据列
        self.Base = self.OM.LoadObj('./Local/TrainAction_Buffer.pkl')
        self.actions = self.Base.actions
        self.solutions = self.Base.solutions
        self.activatings = self.Base.activatings

        former = self.Data.iloc[:,0:self.solution_start]
        latter = self.Data.iloc[:,self.solution_start:self.col-2]
        last = self.Data.iloc[:,self.col-2:self.col]

        for i in range (former.shape[1],self.Base.solution_start):
            former[self.Base.Data.iloc[:,i].name] = 0

        for i in range (former.shape[1]+latter.shape[1],self.Base.col):
            latter[self.Base.Data.iloc[:,i].name] = 0

        self.Data = pd.concat([former,latter,last],axis=1)

        self.col = self.Data.shape[1]
        self.solution_start = self.Base.solution_start


    def AddSample (self,sample): #新增样本
        self.Data = pd.concat([self.Data,sample])
        self.datasize += 1
        self.next_id += 1